<template>
  <div class="app-container">
    <el-dialog :model-value="itemsVisible" title="选择政府单位" :append-to-body="true" width="80%" height="1000px" @close="closeBtn">
      <div class="filter-container">
        <el-form :inline="true" :model="listQuery" class="demo-form-inline">
          <el-form-item prop="governmentType" label="政府单位类型">
            <el-radio-group v-model="listQuery.governmentType">
              <el-radio-button label="">全部</el-radio-button>
              <el-radio-button v-for="(item,index) in getEnumList('GovernmentType')" :label="item.key" :key="index">{{item.value}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="governmentCode" label="政府单位编码">
            <el-input v-model="listQuery.governmentCode" placeholder="政府单位编码" />
          </el-form-item>
          <el-form-item prop="governmentName" label="政府单位名称">
            <el-input v-model="listQuery.governmentName" placeholder="政府单位名称" />
          </el-form-item>
          <el-form-item prop="state" label="状态">
            <el-radio-group v-model="listQuery.state" disabled>
              <el-radio-button label="">全部</el-radio-button>
              <el-radio-button v-for="(item,index) in getEnumList('CommonEnableDisable')" :label="item.key" :key="index">{{item.value}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="Search" @click="handleFilter">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="app-container">
        <el-table v-loading="listLoading"
                  :data="organizationList"
                  border
                  fit
                  highlight-current-row
                  tooltip-effect="dark"
                  style="width: 100%; margin-bottom: 20px;">
          <el-table-column align="center" type="index" label="序号" width="60px" />
          <el-table-column align="center" label="政府单位类型" prop="governmentType" min-width="105px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ showEnumLabel('GovernmentType', row.governmentType) }}</template>
          </el-table-column>
          <el-table-column align="center" label="政府单位编码" prop="governmentCode" min-width="105px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ row.governmentCode }}</template>
          </el-table-column>
          <el-table-column align="center" label="政府单位名称" prop="governmentName" min-width="105px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ row.governmentName }}</template>
          </el-table-column>
          <el-table-column align="center" label="状态" prop="state" min-width="80px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ showEnumLabel('CommonEnableDisable', row.state) }}</template>
          </el-table-column>
          <el-table-column align="center" label="备注" prop="remark" min-width="80px" sortable show-overflow-tooltip>
            <template v-slot="{row}">{{ row.remark }}</template>
          </el-table-column>
          <el-table-column align="center" label="创建时间" width="180px" prop="createTime" sortable show-overflow-tooltip>
            <template v-slot="{row}"><DateTag :timestamp="row.createTime" /></template>
          </el-table-column>
          <el-table-column align="center" label="最后修改时间" width="180px" prop="updateTime" sortable show-overflow-tooltip>
            <template v-slot="{row}"><DateTag :timestamp="row.updateTime" /></template>
          </el-table-column>
          <el-table-column label="选择" width="150" align="center" fixed="right">
            <template v-slot="{row}">
              <el-radio :label="row.id" v-model="tableRadio" border @change="handleCurrentChange($event, row)">选择</el-radio>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-model:total="total" v-model:page="listQuery.pageNum" v-model:limit="listQuery.pageSize" @pagination="getList" class="pagination-model" />
        <div style="text-align:right;margin: 1.875rem 0;">
          <el-button type="danger" @click="closeBtn">关闭</el-button>
          <el-button type="primary" @click="onSureBtn">确定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import {ref, watch} from 'vue'
import {doGet} from "@/config/http";
import {svc, getEnumList, alertErrorMsg, showEnumLabel} from "@/config/common";

const props = defineProps({
  itemsVisible: {
    required: true,
    type: Boolean,
  },
  state: {
    required: false,
    type: String,
  },
})

let organizationList = ref([])
let tableRadio = ref('')
let listLoading = ref(true)
let total = ref(0)
let listQuery = ref({
  pageNum: 1,
  pageSize: 10,
  sortField: 'id',
  sortType: 'desc',
  governmentType: null,
  governmentCode: null,
  governmentName: null,
  state: '1',
  createTimeStart: null,
  createTimeEnd: null,
  updateTimeStart: null,
  updateTimeEnd: null,
})
let radioRow = null

const emit = defineEmits(['close', 'governmentSelect'])

watch(() => [props.itemsVisible, props.state], async vals => {
  let visible = vals[0]
  let state = vals[1]
  if (state) {
    listQuery.value.state = state
  } else {
    listQuery.value.state = null
  }
  if (visible === true) {
    await getList()
  }
})

// 获取数据
async function getList() {
  listLoading.value = true
  let res = await doGet(svc, '/government/page', listQuery.value)
  listLoading.value = false
  total.value = parseInt(res['total'])
  organizationList.value = res['records']
}

// 查询
async function handleFilter() {
  listQuery.value['pageNum'] = 1
  await getList()
}

// 关闭弹层
function closeBtn() {
  emit('close')
}

// 选择
function handleCurrentChange(val, row) {
  radioRow = row
}

// 点确定
function onSureBtn() {
  if (!radioRow) {
    alertErrorMsg('请选择政府单位')
    return
  }
  emit('governmentSelect', radioRow)
  emit('close')
}
</script>
<style>
.pagination-model {
  right: 36px !important;
}
</style>
